package com.knowledgegraph.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.knowledgegraph.entity.KnowledgeGraph;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 知识图谱Mapper接口
 * 
 * @author system
 * @since 2024-01-01
 */
@Mapper
public interface KnowledgeGraphMapper extends BaseMapper<KnowledgeGraph> {

    /**
     * 根据创建者ID查询图谱列表
     * 
     * @param creatorId 创建者ID
     * @return 图谱列表
     */
    @Select("SELECT * FROM kg_knowledge_graph WHERE creator_id = #{creatorId} AND deleted = 0 ORDER BY update_time DESC")
    List<KnowledgeGraph> findByCreatorId(@Param("creatorId") Long creatorId);

    /**
     * 查询公开的图谱列表
     * 
     * @param page 分页参数
     * @return 图谱分页列表
     */
    @Select("SELECT * FROM kg_knowledge_graph WHERE is_public = 1 AND status = 1 AND deleted = 0 ORDER BY update_time DESC")
    IPage<KnowledgeGraph> findPublicGraphs(Page<KnowledgeGraph> page);

    /**
     * 根据分类查询图谱数量
     * 
     * @param category 分类
     * @return 数量
     */
    @Select("SELECT COUNT(*) FROM kg_knowledge_graph WHERE category = #{category} AND deleted = 0")
    Integer countByCategory(@Param("category") String category);

    /**
     * 统计用户的图谱数量
     * 
     * @param creatorId 创建者ID
     * @return 数量
     */
    @Select("SELECT COUNT(*) FROM kg_knowledge_graph WHERE creator_id = #{creatorId} AND deleted = 0")
    Integer countByCreatorId(@Param("creatorId") Long creatorId);
}